package com.tongfusoft.document.manager.role.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.entity.Privilege;
import com.tongfusoft.document.entity.RoleHasPrivileges;
import com.tongfusoft.document.manager.role.service.IRolePrivilegeService;
import com.tongfusoft.spring.web.core.data.User;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("rolePrivilegeService")
public class RolePrivilegeServiceImpl extends DataTemplateServiceImpl implements IRolePrivilegeService{

	/**
	 * 新增角色权限
	 */
	@Override
	public void addRolePrivileges(User user, String roleId, List<String> privileges) {
		List<RoleHasPrivileges> list = new ArrayList<RoleHasPrivileges>();
		for(String priId : privileges){
			RoleHasPrivileges rp = new RoleHasPrivileges();
			
			rp.setCreatedBy(user.getPkValue());
			rp.setLastUpdBy(user.getPkValue());
			rp.setLastUpdDate(new Timestamp(new Date().getTime()));
			rp.setCreatedDate(new Timestamp(new Date().getTime()));
			rp.setStatus("1");
			
			rp.setRoleId(roleId);
			rp.setPriId(priId);
			
			rp.setRpId(UIDGeneratorService.getUUID());
			
			list.add(rp);
		}
		
		for(RoleHasPrivileges rp : list){
			super.insert("addRoleHasPrivilege", rp);
		}
		
	}

	/**
	 * 删除角色权限
	 */
	@Override
	public void delRolePrivilege(User user, String rpId) {
		RoleHasPrivileges rhp =new RoleHasPrivileges();
		rhp.setRpId(rpId);
		this.delete("removeRoleHasPrivilege", rhp);
	}

	/**
	 * 获取角色包含的权限列表
	 */
	@Override
	public PageInfo<RoleHasPrivileges> privilegesListByRoleIdPageTable(int pageIndex,int pageSize,String roleId){
		 return super.findPage("privilegesListByRoleId", roleId, pageSize, pageIndex);
	}
	
	/**
	 * 获取角色可选的权限列表
	 */
	@Override
	public List<Privilege> altPrivilegesListByRoleId(String roleId){
		return super.selectList("altPrivilegesListByRoleId", roleId);
	}

}
